package com.daydayup.learn;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class AutoTestClient {

    public static void main(String[] args) {
        // 改为通过全限定类名传入尽量,参数也通过不定参数传入
        CalcHelpDemo calcHelpDemo = new CalcHelpDemo();
        int para1 = 10;
        int para2 = 0;
        Method[] methods = calcHelpDemo.getClass().getMethods();
        AtomicInteger bugCount = new AtomicInteger();
        String filePath = "BugReport"+(DateUtil.format(new Date(),"yyyyMMddHHmmss"))+".txt";

        for (int i = 0; i < methods.length; i++) {
            if (methods[i].isAnnotationPresent(MyTest.class)) {
                try {
                    methods[i].invoke(calcHelpDemo, para1, para2);
                } catch (Exception e) {
                    bugCount.getAndIncrement();
                    log.info("异常名称:{},异常原因:{}",e.getCause().getClass().getSimpleName(),e.getCause().getMessage());
                    FileUtil.writeString(methods[i].getName()+"\t出现了异常\n",filePath,"UTF-8");
                    FileUtil.appendString("异常名称:"+e.getCause().getClass().getSimpleName()+"\n",filePath,"UTF-8");
                    FileUtil.appendString("异常原因:"+e.getCause().getMessage()+"\n",filePath,"UTF-8");
                } finally {
                    FileUtil.appendString("异常数:"+bugCount.get()+"\n",filePath,"UTF-8");
                }
            }
        }
    }
}
